package Core 
{
	import com.greensock.TweenLite;
	
	import Common.Constants;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author Serg de Adelantado
	 */
	public class ImageTools extends Sprite 
	{
		private const HEIGHT:int = 30;
		private const ICON_SIZE:int = 20;
		private const ICON_Y:int = (HEIGHT - ICON_SIZE) / 2;
		
		private var FRotateCW:Sprite;
		private var FRotateCCW:Sprite;		
		private var FZoom:Sprite;
		private var FImage:ImageContainer;
		private var FContainer:Sprite;
		
		public function ImageTools() 
		{
			super();
			FContainer = new Sprite();
			
			FZoom = MakeIcon(ZoomIcon);
			FRotateCW = MakeIcon(CWIcon);			
			FRotateCCW = MakeIcon(CCWIcon);			
			
			FContainer.addChild(FZoom);
			FContainer.addChild(FRotateCW);
			FContainer.addChild(FRotateCCW);
			addChild(FContainer);
			
			FRotateCCW.y = FRotateCW.y = FZoom.y = ICON_Y;
			FRotateCW.addEventListener(MouseEvent.MOUSE_DOWN, RotateCW);
			FRotateCCW.addEventListener(MouseEvent.MOUSE_DOWN, RotateCCW);
			FZoom.addEventListener(MouseEvent.MOUSE_DOWN, OnZoom);
			focusRect = false;
			FContainer.focusRect = false;
			FRotateCW.focusRect = false;
			FRotateCCW.focusRect = false;
		}
			
		private function MakeIcon(AClass:Class):Sprite
		{
			var vSprite:Sprite = new Sprite();
			var vIcon:Bitmap = new Bitmap(new AClass());
			ImageUtils.Resize(vIcon, ICON_SIZE, ICON_SIZE);
			vIcon.smoothing = true;
			vSprite.addChild(vIcon);
			return vSprite;
		}
		
		private function RotateCW(e:MouseEvent):void 
		{
			FImage.Rotation += 90;
		}
		
		private function RotateCCW(e:MouseEvent):void 
		{
			FImage.Rotation -= 90;
		}		
		
		private function OnZoom(e:MouseEvent):void 
		{
			FImage.dispatchEvent(new MouseEvent(MouseEvent.DOUBLE_CLICK));
		}

		public function set Image(AImage:ImageContainer):void
		{
			FImage = AImage;
			FImage.addEventListener(MouseEvent.MOUSE_OVER, OnImageMouseOver);
			FImage.addEventListener(MouseEvent.MOUSE_OUT, OnImageMouseOut);
		}
		
		private function OnImageMouseOver(e:MouseEvent):void 
		{
			TweenLite.killTweensOf(FContainer);
			TweenLite.to(FContainer, 0.5, { y:0 } );
		}
		
		private function OnImageMouseOut(e:MouseEvent):void 
		{
			TweenLite.killTweensOf(FContainer);
			TweenLite.to(FContainer, 0.5, { y:HEIGHT } );
		}
		
		public function get Height():int
		{
			return HEIGHT;
		}
		
		public function set Width(AWidth:int):void
		{
			graphics.clear();
			graphics.beginFill(0, 0);
			graphics.drawRect(0, 0, AWidth, HEIGHT);
			graphics.endFill();
			
			FContainer.graphics.clear();
			FContainer.graphics.beginFill(Constants.IMAGE_TOOLS_BACKGROUND, Constants.IMAGE_TOOLS_ALPHA);
			FContainer.graphics.drawRect(0, 0, AWidth, HEIGHT);
			FContainer.graphics.endFill();			
			FZoom.x = (AWidth - FZoom.width) / 2;
			FRotateCCW.x = FZoom.x - FRotateCCW.width - 5;
			FRotateCW.x = FZoom.x + FZoom.width + 5;
			FContainer.y = HEIGHT;
		}
	}
}